package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.bus.info.SupplierSystemOrderBusInfoDTO;
import com.ctshk.rpc.booking.dto.system.bus.info.SupplierSystemOrderBusTicketInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAirTicket;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderBus;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderBusTicket;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderBusMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderBusTicketMapper;
import com.ctshk.rpc.booking.req.system.bus.add.SupplierSystemOrderBusTicketAddReq;
import com.ctshk.rpc.booking.req.system.bus.edit.SupplierSystemOrderBusEditReq;
import com.ctshk.rpc.booking.req.system.bus.add.SupplierSystemOrderBusAddReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderBusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统巴士票订单 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierSystemOrderBusServiceImpl implements ISupplierSystemOrderBusService {
    @Autowired
    SupplierSystemOrderBusMapper supplierSystemOrderBusMapper;
    @Autowired
    SupplierSystemOrderBusTicketMapper supplierSystemOrderBusTicketMapper;

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderBusEditReq> supplierSystemOrderBusEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderBusEditReqList)) {
            supplierSystemOrderBusEditReqList.forEach(busEditReq -> {
                UpdateWrapper<SupplierSystemOrderBus> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderBus::getId, busEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderBus::getCustomerRemark, busEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderBus::getSupplierRemark, busEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderBus::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderBus::getGmtModified, nowDateTime);
                supplierSystemOrderBusMapper.update(new SupplierSystemOrderBus(), updateWrapper);
                busEditReq.getSupplierSystemOrderBusTicketEditReqList().forEach(busTicketReq -> {
                    UpdateWrapper<SupplierSystemOrderBusTicket> updateTicketWrapper = new UpdateWrapper<>();
                    updateTicketWrapper.lambda().eq(SupplierSystemOrderBusTicket::getId, busTicketReq.getId());
                    updateTicketWrapper.lambda().set(SupplierSystemOrderBusTicket::getSinglePrice, busTicketReq.getSinglePrice());
                    updateTicketWrapper.lambda().set(SupplierSystemOrderBusTicket::getModifiedId, userId);
                    updateTicketWrapper.lambda().set(SupplierSystemOrderBusTicket::getGmtModified, nowDateTime);
                    supplierSystemOrderBusTicketMapper.update(null, updateTicketWrapper);
                });
            });
        }
    }

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderBusAddReq> supplierSystemOrderBusAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderBusAddReqList)) {
            supplierSystemOrderBusAddReqList.forEach(busAddReq -> {
                SupplierSystemOrderBus supplierSystemOrderBus = EntityUtil.copy(busAddReq, SupplierSystemOrderBus.class);
                supplierSystemOrderBus.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderBus.setOrderId(id);
                supplierSystemOrderBus.setCreateId(userId);
                supplierSystemOrderBus.setGmtCreate(nowDateTime);
                supplierSystemOrderBus.setModifiedId(userId);
                supplierSystemOrderBus.setGmtModified(nowDateTime);
                supplierSystemOrderBus.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderBusMapper.insert(supplierSystemOrderBus);
                //添加旅客
                List<SupplierSystemOrderBusTicketAddReq> supplierSystemOrderBusTicketAddReqList = busAddReq.getSupplierSystemOrderBusTicketAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderBusTicketAddReqList)) {
                    supplierSystemOrderBusTicketAddReqList.forEach(busTicketAdd -> {
                        SupplierSystemOrderBusTicket supplierSystemOrderBusTicket = EntityUtil.copy(busTicketAdd, SupplierSystemOrderBusTicket.class);
                        supplierSystemOrderBusTicket.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderBusTicket.setOrderId(id);
                        supplierSystemOrderBusTicket.setBusId(supplierSystemOrderBus.getId());
                        supplierSystemOrderBusTicket.setCreateId(userId);
                        supplierSystemOrderBusTicket.setGmtCreate(nowDateTime);
                        supplierSystemOrderBusTicket.setModifiedId(userId);
                        supplierSystemOrderBusTicket.setGmtModified(nowDateTime);
                        supplierSystemOrderBusTicket.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderBusTicketMapper.insert(supplierSystemOrderBusTicket);
                    });
                }
            });
        }
    }

    @Override
    public List<SupplierSystemOrderBusInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderBus> busQueryWrapper = new QueryWrapper<>();
        busQueryWrapper.lambda().eq(SupplierSystemOrderBus::getOrderId, id);
        List<SupplierSystemOrderBus> supplierSystemOrderBusList = supplierSystemOrderBusMapper.selectList(busQueryWrapper);
        List<SupplierSystemOrderBusInfoDTO> supplierSystemOrderBusInfoDTOList = EntityUtil.copyList(supplierSystemOrderBusList, SupplierSystemOrderBusInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderBusInfoDTOList)) {
            supplierSystemOrderBusInfoDTOList.forEach(busCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderBusTicket> busTicketQueryWrapper = new QueryWrapper<>();
                busTicketQueryWrapper.lambda().eq(SupplierSystemOrderBusTicket::getOrderId, id);
                List<SupplierSystemOrderBusTicket> supplierSystemOrderBusTickets = supplierSystemOrderBusTicketMapper.selectList(busTicketQueryWrapper);
                List<SupplierSystemOrderBusTicketInfoDTO> supplierSystemOrderBusTicketInfoDTOS = EntityUtil.copyList(supplierSystemOrderBusTickets, SupplierSystemOrderBusTicketInfoDTO.class);
                busCheckinDTO.setSupplierSystemOrderBusTicketInfoDTOList(supplierSystemOrderBusTicketInfoDTOS);
            });
        }
        return supplierSystemOrderBusInfoDTOList;
    }
}
